<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use EasyWeChat\Factory;
use app\admin\model\SystemAdmin;
use think\facade\View;
use app\BaseController;
use think\facade\Db;
/**
 * 微信登录模型
 */
class Wechat extends BaseController
{
    public function getApp()
    {
        $app_id = config('wechat.app_id') ?? 'wx9564eaec1bbe4911';
        $secret = config('wechat.secret') ?? 'c61fc2765392ab445de1603fa1c164fe';
        $token = config('wechat.token') ?? 'Guo';
        $config = [
            'app_id' => $app_id,
            'secret' => $secret,
            'token'  => $token
        ];
        return Factory::officialAccount($config);
    }

    public function serve()
    {
        $app = $this->getApp();
        $app->server->push(function($message){
            switch ($message['MsgType']) {
                case 'event':
                    return '收到事件信息';
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                default:
                    return '收到其它消息';
                    break;
            }
        });
        $app->server->serve()->send();

    }

    public function see()
    {
        $uid = $this->request->param('uid');
        if(empty($uid) || !is_numeric($uid)){
            return json([
                'code' => 401,
                'msg' => '参数错误'
            ]);
        }
        $app_id = config('wechat.app_id') ?? 'wx9564eaec1bbe4911';
        $secret = config('wechat.secret') ?? 'c61fc2765392ab445de1603fa1c164fe';
        $token = config('wechat.token') ?? 'Guo';
        $config = [
            'app_id' => $app_id,
            'secret' => $secret,
            'token'  => $token,
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/wechat/wechat/cc',
            ],
        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        // 获取用户信息
        if (empty(session('wechat_user'))) {
            session('target_url','/wechat/wechat/see?uid='.$uid);
            return $oauth->redirect();
        }
        $user = session('wechat_user');
        Db::startTrans();
        try {
            $dt = SystemAdmin::where('id',$uid)->find();
            if(empty($dt['open_id'])){
                SystemAdmin::update(['id'=>$uid,'open_id'=> $user['id']]);
            }
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
        }
        return View('see',[
            'user' => $user,
            'uid'  => $uid
        ]);
    }

    public function cc()
    {
        $app = $this->getApp();
        $oauth = $app->oauth;
        $user = $oauth->user();
        session('wechat_user',$user->toArray());
        $targetUrl = empty(session('target_url')) ? '/' : session('target_url');
        header('location:'. $targetUrl);
    }
}
